package com.ruoyi.project.jiaqin.apply.service.impl;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.apply.domain.CompanyApply;
import com.ruoyi.project.jiaqin.apply.mapper.CompanyApplyMapper;
import com.ruoyi.project.jiaqin.apply.service.ICompanyApplyService;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.mapper.CompanyMapper;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 企业申请Service业务层处理
 *
 * @author admin
 * @date 2020-12-24
 */
@Service
public class CompanyApplyServiceImpl implements ICompanyApplyService
{
    @Autowired
    private CompanyApplyMapper companyApplyMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private ICompanyService companyService;

    /**
     * 查询企业申请
     *
     * @param id 企业申请ID
     * @return 企业申请
     */
    @Override
    public CompanyApply selectCompanyApplyById(String id)
    {
        return companyApplyMapper.selectCompanyApplyById(id);
    }

    /**
     * 查询企业申请列表
     *
     * @param companyApply 企业申请
     * @return 企业申请
     */
    @Override
    public List<CompanyApply> selectCompanyApplyList(CompanyApply companyApply)
    {
        return companyApplyMapper.selectCompanyApplyList(companyApply);
    }

    @Override
    @Transactional
    public int examYe(CompanyApply companyApply) {
        companyApply.setStatus(1);//设置申请状态
        Company company = new Company();
        company.setId(UUID.randomUUID().toString().replace("-",""));
        company.setMemberId(companyApply.getMemberId());
        company.setCompanyName(companyApply.getCompanyName());
        company.setCompanyAddr(companyApply.getCompanyAddr());
//        company.setCompanyAvatar("http://imgjq.szyujingkeji.com/companydefaultimg.jpg");
//        company.setCompanyAvatar("https://imgjq.szyujingkeji.com/2021/10/20/d394e8d5ef9d4fced9e9620b0d7ec545.jpg");
        Member memberById = memberMapper.selectMemberById(companyApply.getMemberId());
        company.setCompanyAvatar(memberById.getHeadpic());
        company.setCompanyPhone(companyApply.getPhoneNum());
        company.setProvince(companyApply.getProvince());
        company.setCity(companyApply.getCity());
        company.setArea(companyApply.getArea());
        company.setProvinceName(companyApply.getProvinceName());
        company.setCityName(companyApply.getCityName());
        company.setAreaName(companyApply.getAreaName());
        company.setIndustry(companyApply.getIndustry());
        company.setLongitude(companyApply.getLongitude());
        company.setLatitude(companyApply.getLatitude());
        companyService.insertCompany(company);
        Member member = new Member();
        member.setId(companyApply.getMemberId());
        member.setIsCompany(1);
        memberMapper.updateMember(member);
        return companyApplyMapper.updateCompanyApply(companyApply);
    }

    /**
     * 新增企业申请
     *
     * @param companyApply 企业申请
     * @return 结果
     */
    @Override
    public int insertCompanyApply(CompanyApply companyApply)
    {
        companyApply.setId(UUID.randomUUID().toString().replace("-",""));
        return companyApplyMapper.insertCompanyApply(companyApply);
    }

    /**
     * 修改企业申请
     *
     * @param companyApply 企业申请
     * @return 结果
     */
    @Override
    public int updateCompanyApply(CompanyApply companyApply)
    {
        return companyApplyMapper.updateCompanyApply(companyApply);
    }

    /**
     * 删除企业申请对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCompanyApplyByIds(String ids)
    {
        return companyApplyMapper.deleteCompanyApplyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除企业申请信息
     *
     * @param id 企业申请ID
     * @return 结果
     */
    @Override
    public int deleteCompanyApplyById(String id)
    {
        return companyApplyMapper.deleteCompanyApplyById(id);
    }

    @Override
    public List<CompanyApply> findCompanyApplyByMemberIdAndStatus(String memberId) {
        return companyApplyMapper.findCompanyApplyByMemberIdAndStatus(memberId);
    }
}
